]> git.r.bdr.sh - rbdr/map/blame - Map/Presentation/Base Components/MapRender/MapOpportunities.swift
Use comma separated strings
[rbdr/map] / Map / Presentation / Base Components / MapRender / MapOpportunities.swift
CommitLineData
98f09799
RBR
1/*
2 Copyright (C) 2024 Rubén Beltrán del Río
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see https://map.tranquil.systems.
16 */
5e8ff485
RBR
17import SwiftUI
18
19struct MapOpportunities: View {
20
5e8ff485
RBR
21 let mapSize: CGSize
22 let lineWidth: CGFloat
23 let vertexSize: CGSize
24 let opportunities: [Opportunity]
25
26 let arrowheadSize = CGFloat(10.0)
27
5e8ff485
RBR
28 var body: some View {
29 ForEach(opportunities, id: \.id) { edge in
30 Path { path in
31
32 // First we transform edges from percentage to map coordinates
33 let origin = CGPoint(x: w(edge.origin.x), y: h(edge.origin.y))
34 let destination = CGPoint(x: w(edge.destination.x), y: h(edge.destination.y))
35
36 let multiplier = CGFloat(edge.destination.x > edge.origin.x ? 1.0 : -1.0)
37 let upperAngle = -CGFloat.pi / 4.0
38 let lowerAngle = CGFloat.pi / 4.0
39
40 let offsetOrigin = CGPoint(x: origin.x + multiplier * (vertexSize.width / 2.0), y: origin.y)
41 let offsetDestination = CGPoint(
42 x: destination.x - multiplier * (vertexSize.width / 2.0), y: destination.y)
43
44 path.move(to: offsetOrigin)
45 path.addLine(to: offsetDestination)
46
47 path.move(to: offsetDestination)
48 path.addLine(
49 to: CGPoint(
50 x: offsetDestination.x - multiplier * arrowheadSize * cos(upperAngle),
51 y:
52 offsetDestination.y - multiplier * arrowheadSize * sin(upperAngle)))
53
54 path.move(to: offsetDestination)
55 path.addLine(
56 to: CGPoint(
57 x: offsetDestination.x - multiplier * arrowheadSize * cos(lowerAngle),
58 y:
59 offsetDestination.y - multiplier * arrowheadSize * sin(lowerAngle)))
60
61 path.move(to: offsetDestination)
62 path.closeSubpath()
63 }.applying(
64 CGAffineTransform(translationX: vertexSize.width / 2.0, y: vertexSize.height / 2.0)
e2c37ac1
RBR
65 ).strokedPath(StrokeStyle(lineWidth: lineWidth / 4, dash: [10.0])).stroke(
66 Color.map.opportunityColor)
5e8ff485
RBR
67 }
68 }
69
70 func h(_ dimension: CGFloat) -> CGFloat {
71 max(0.0, min(mapSize.height, dimension * mapSize.height / 100.0))
72 }
73
74 func w(_ dimension: CGFloat) -> CGFloat {
75 max(0.0, min(mapSize.width, dimension * mapSize.width / 100.0))
76 }
77}
78
e2c37ac1
RBR
79#Preview {
80 MapOpportunities(
81 mapSize: CGSize(width: 400.0, height: 400.0), lineWidth: 1.0,
82 vertexSize: CGSize(width: 25.0, height: 25.0),
83 opportunities: [
84 Opportunity(id: 1, origin: CGPoint(x: 2.0, y: 34.0), destination: CGPoint(x: 23.0, y: 76.2))
85 ])
5e8ff485 86}